package code1;

import java.util.Objects;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/21 20:28
 * 符合类型的hashCode
 */
public class Student {
    int grade;
    int cls;
    String firstName;
    String lastName;

    public Student(int grade, int cls, String firstName, String lastName) {
        this.grade = grade;
        this.cls = cls;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public boolean equals(Object o){

        if(this == o) {
            return true;
        }

        if(o == null) {
            return false;
        }

        if(getClass() != o.getClass()) {
            return false;
        }

        Student another = (Student)o;
        return this.grade == another.grade &&
                this.cls == another.cls &&
                this.firstName.toLowerCase().equals(another.firstName.toLowerCase()) &&
                this.lastName.toLowerCase().equals(another.lastName.toLowerCase());
    }


    //自定义复合类型的hashCode
    @Override
    public int hashCode() {
       int B = 31;
       int hash = 0;
       hash = hash * B + grade;
       hash = hash * B + cls;
       hash = hash + B +firstName.toLowerCase().hashCode();
       hash = hash + B +lastName.toLowerCase().hashCode();

       return hash;
    }
}
